ggplot(data = mpg) +geom_point(mapping =aes(x = displ, y = hwy), color ="blue")
Let’s add useful headings
ggplot(data = mpg) +geom_point(mapping =aes(x = displ, y = hwy), color ="blue") +labs(title ="Relationship between engine displacement and highway miles per gallon",x ="Engine displacement (L)",y ="Highway miles per gallon" )
Let’s add useful headings
Let’s clean this up
ggplot(data = mpg) +geom_point(mapping =aes(x = displ, y = hwy), color ="blue") +theme_minimal() +labs(title ="Relationship between engine displacement and highway miles per gallon",x ="Engine displacement (L)",y ="Highway miles per gallon" )
Let’s clean this up
Creating your own theme
ggplot(data = mpg) +geom_point(mapping =aes(x = displ, y = hwy, color = class)) +theme(legend.position ="bottom",panel.grid =element_blank(),panel.background =element_blank(),plot.title.position ="plot",plot.title =element_text(face ="bold") ) +labs(title ="Relationship between engine displacement and highway miles per gallon by class",x ="Engine displacement (L)",y ="Highway miles per gallon",color ="Class" )
Creating your own theme
EXERCISES
What’s gone wrong with this code? Why are the points not blue?
ggplot(data = mpg) +geom_point(mapping =aes(x = displ, y = hwy, color ="blue"))
EXERCISES
Which variables in mpg are categorical? Which variables are continuous?
Map a continuous variable to color, size, and shape. How do these aesthetics behave differently for categorical vs. continuous variables?
What happens if you map the same variable to multiple aesthetics?
EXERCISES
What does the stroke aesthetic do? What shapes does it work with?
What happens if you map an aesthetic to something other than a variable name, like aes(colour = displ < 5)? Note, you’ll also need to specify x and y.